﻿using System.Collections.Generic;
using System.IO;
using Commons.Collections;
using NVelocity;
using NVelocity.App;

namespace Blader.UI
{
    public class NvEngine
    {
        private static readonly NvEngine instance = new NvEngine();

        private readonly VelocityEngine velocityEngine;

        private NvEngine()
        {
            velocityEngine = new VelocityEngine();

            ExtendedProperties props = new ExtendedProperties();
            //props.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "");
            velocityEngine.Init(props);
        }

        public static NvEngine GetInstance()
        {
            return instance;
        }

        public virtual string Process(string source, Dictionary<string, object> context)
        {
            VelocityContext velocityContext = new VelocityContext();
            foreach (KeyValuePair<string, object> pair in context)
            {
                velocityContext.Put(pair.Key, pair.Value);
            }
            StringWriter writer = new StringWriter();
            velocityEngine.Evaluate(velocityContext, writer, "", source);
            return writer.GetStringBuilder().ToString();
        }
    }
}
